package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Student;
import com.example.springboot.entity.Teacher;
import com.example.springboot.entity.User;
import com.example.springboot.service.StudentService;
import com.example.springboot.service.SysRoleService;
import com.example.springboot.service.TeacherService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;
    @Resource
    private SysRoleService sysRoleService;


    //重写新增学生用户
    /**
     * 添加学生信息
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody Teacher teacher) {
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Teacher::getUsername,teacher.getUsername());
        Teacher one=teacherService.getOne(queryWrapper);
        if (one!=null){
            return Result.error("-1", "账户已存在");
        }
        teacherService.addTea(teacher);
        return Result.success();
    }

    /**
     * 学生登录
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user, HttpSession session) {
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        Object o = teacherService.TesLogin(user.getUsername(), user.getPassword());
        if (o != null) {
            System.out.println(o);
            //存入session
            session.setAttribute("Identity", "teacher");
            session.setAttribute("User", o);
            return Result.success(o);
        } else {
            return Result.error("-1", "用户名或密码错误");
        }
    }
    /**
     * 更新学生信息
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody Teacher teacher) {
        boolean i = teacherService.updateById(teacher);
        if (i ) {
            return Result.success();
        } else {
            return Result.error("-1", "更新失败");
        }
    }

    /**
     * 删除学生信息
     */
    @DeleteMapping("/delete/{userId}")
    public Result<?> delete(@PathVariable Long userId) {
        boolean i = teacherService.removeById(userId);
        if (i ) {
            return Result.success();
        } else {
            return Result.error("-1", "删除失败");
        }
    }

    /**
     * 查找学生信息
     */
    @GetMapping("/findList")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page page = teacherService.findList(pageNum, pageSize, search);
        if (page != null) {
            return Result.success(page);
        } else {
            return Result.error("-1", "查询失败");
        }
    }
    //根据班级查找教师信息
    @GetMapping("/classname/{className}")
    public Result<?> findPage(@PathVariable String className) {
        QueryWrapper<Teacher> qw = new QueryWrapper<>();
        qw.eq("classname", className);  // 精确匹配
        Teacher teacher=teacherService.getOne(qw);  // 返回单个 Teacher 对象
        return Result.success(teacher);
    }


}
